package com.module.reading;

import com.module.netty.decoder.NettyDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class NettyInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private NettyHandler nettyHandler;

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        //IdleStateHandler心跳机制,如果超时触发Handle中userEventTrigger()方法
        pipeline.addLast("idleStateHandler",
                new IdleStateHandler(25, 0, 0, TimeUnit.MINUTES));
        //自定义解码规则
        pipeline.addLast(new NettyDecoder());
        //自定义Handler
        pipeline.addLast("nettyHandler", nettyHandler);
    }
}
